package com.itdr.protal.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.Order;
import com.itdr.pojo.Payinfo;
import com.itdr.protal.mapper.OrderMapper;
import com.itdr.protal.mapper.PayinfoMapper;
import com.itdr.protal.service.IPayinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itdr.utils.ZFBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@Service
public class PayinfoServiceImpl extends ServiceImpl<PayinfoMapper, Payinfo> implements IPayinfoService {
    @Resource
    PayinfoMapper payinfoMapper;

    @Resource
    OrderMapper orderMapper;

    @Autowired
    ZFBUtil zfbUtil;

    private Payinfo getPayInfo(Integer uid,Map<String, String> parMap){
        Payinfo payinfo = new Payinfo();
        payinfo.setOrderNo(Long.valueOf(parMap.get("out_trade_no")));
        payinfo.setPayPlatform(1);
        payinfo.setPlatformNumber(parMap.get("trade_no"));
        payinfo.setPlatformStatus(parMap.get("trade_status"));
        payinfo.setCreateTime(new Date());
        payinfo.setUpdateTime(new Date());
        payinfo.setUserId(uid);
        return payinfo;
    }

    @Override
    public ServerResponse zf(String shopName, String orderNo, BigDecimal totalPrice,String returnUrl) {
        AlipayTradePagePayResponse alipayTradePagePayResponse = zfbUtil.payPage(shopName, orderNo, totalPrice, returnUrl);
        if(!ResponseChecker.success(alipayTradePagePayResponse)){
            return ServerResponse.toFail(Const.PayEnum.FAIL_PAY_ORDER.getCode(),Const.PayEnum.FAIL_PAY_ORDER.getDesc());
        }
        return ServerResponse.toSuccess(alipayTradePagePayResponse.getBody());
    }

    @Override
    public boolean zfbYQ(Map<String, String> parMap) {
        boolean b = zfbUtil.zfNotify(parMap);
        if(b){
            // 记录支付信息
            Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("order_no", parMap.get("out_trade_no")));
            if(order != null){
                Payinfo payInfo = getPayInfo(order.getUserId(),parMap);
                payinfoMapper.insert(payInfo);
                // 修改订单状态
                Order o = new Order();
                o.setStatus(20);
                o.setUpdateTime(new Date());
                // 日期字符串改date
                DateTime gmt_payment = DateUtil.parse(parMap.get("gmt_payment"));
                o.setPaymentTime(gmt_payment);
                orderMapper.update(o,new UpdateWrapper<Order>().eq("order_no",parMap.get("out_trade_no")));
            }else{
                throw new RuntimeException("订单"+parMap.get("out_trade_no")+"不存在");
            }
        }
        return b;
    }
}
